<?php
include("../Connections/connect_admin.php");
session_start();
$session_id=$_SESSION['username']; //$session id
$path = "../Images/Img_User/";

	$valid_formats = array("jpg", "png", "gif", "bmp");
	if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
		{
			$name = $_FILES['photoimg']['name'];
			$size = $_FILES['photoimg']['size'];
			
			if(strlen($name))
				{
					list($txt, $ext) = explode(".", $name);
					if(in_array($ext,$valid_formats))
					{
					if($size<(120*250))
						{
							$actual_image_name = $session_id.'.'.$ext;
							$tmp = $_FILES['photoimg']['tmp_name'];
							if (file_exists($path.$actual_image_name))
							  {
							 	unlink($path.$actual_image_name);
							  }
							if(move_uploaded_file($tmp, $path.$actual_image_name))
								{
								mysql_query("UPDATE user SET Image='$actual_image_name' WHERE UserName='$session_id'");
								$_SESSION["avatar"]=$actual_image_name;
									echo "<img src='".$path.$actual_image_name."'  class='preview' alt='avatar'>";
								}
							else
								echo "failed";
						}
						else
						echo "Image file size max 30kb";					
						}
						else
						echo "Invalid file format..";	
				}
				
			else
				echo "Please select image..!";
				
			exit;
		}
?>